package irisweb

import (
	"fmt"

	"github.com/kataras/iris"
)

// NewRecoverMiddleware returns a new recover middleware,
// it recovers from panics and logs
// the panic message to the application's logger "Warn" level.
func NewRecoverMiddleware() iris.Handler {
	return func(ctx iris.Context) {
		defer func() {
			if err := recover(); err != nil {
				if ctx.IsStopped() {
					return
				}

				WriteJSONError(ctx, 500, 500, "UnhandlerException", fmt.Sprintf("%s", err))
				// ctx.StopExecution()
			}
		}()

		ctx.Next()
	}
}
